package com.kidback.bbloc259.util;

import android.annotation.SuppressLint;
import android.os.Looper;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * Created by meikai on 2020/06/12.
 */
@SuppressLint("CheckResult")
public class ThreadUtils {

    public static boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }


    public static void postUI(Runnable runnable) {
        Observable.just(0)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        runnable.run();
                    }
                });
    }

    public static void postUIDelay(int milliSeconds, Runnable runnable) {
        Observable.timer(milliSeconds, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        runnable.run();
                    }
                });
    }

}
